package bd;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class Inserts {
	
	public static boolean newDormitorio(String NDormitorio, String activo, String sexoAdmit, String cantPers) {
			
		boolean res = false;
		
		try {		
			String str="INSERT INTO Dormitorios (NDormitorio,Estado,SexoAdmit,Cantidad) VALUES (TO_NUMBER(?),?,?,TO_NUMBER(?))";
			PreparedStatement stmt = DataBaseConection.conn.prepareStatement(str);
			stmt.setString(1, NDormitorio);
			stmt.setString(2, activo);
			stmt.setString(3, sexoAdmit);
			stmt.setString(4, cantPers);
			
			stmt.execute();
		    stmt.close();
		    res=true;
		}catch(Exception e){
			System.out.println(e);
		}
		return res;
	}
	
	public static boolean newCarac(String carac, String precio){
		boolean res = false;
		
		try {				
			String str="INSERT INTO Caracteristicas (NombreServ,Precio) VALUES (?,TO_NUMBER(?))";
			PreparedStatement stmt = DataBaseConection.conn.prepareStatement(str);
			stmt.setString(1, carac);
			stmt.setString(2, precio);

			stmt.execute();
			res = true;
		    stmt.close();
		}catch(Exception e){
			System.out.println(e);
		}
		return res;
	}
	
	public static boolean newCaracDorm(String carac, String nDorm){
		boolean res = false;
		
		try {		
			String str="INSERT INTO ServiciosDormi (NombreServ,NDormitorio) VALUES (?,TO_NUMBER(?))";
			PreparedStatement stmt = DataBaseConection.conn.prepareStatement(str);
			stmt.setString(1, carac);
			stmt.setString(2, nDorm);
			
			stmt.execute();
			res = true;
		    stmt.close();
		}catch(Exception e){
			System.out.println(e);
		}
		return res;
	}
	
	public static void newReserva(String nDoc, String tipoDoc, String nDorm, String arribo, String partida){
		
		try {				
			CallableStatement stmt = DataBaseConection.conn.prepareCall("call insertReserva(?,?,?,?,?)");
			stmt.setString(1, nDoc);
			stmt.setString(2, tipoDoc);
			stmt.setString(3, nDorm);
			stmt.setString(4, arribo);
			stmt.setString(5, partida);			
			
			stmt.execute();	
		    
			stmt.close();
			
			PreparedStatement stmt2 = DataBaseConection.conn.prepareStatement("SELECT PrecioTotal FROM Reserva WHERE Numero = (SELECT COUNT(*) - 1 FROM Reserva)");
			ResultSet result = stmt2.executeQuery();
			result.next();	
			float aux= result.getFloat(1);
			stmt.close();
			
			
			System.out.println("El precio total de la reserva es: "+aux);
		}catch(Exception e){
			System.out.println(e);
		}

	}
	
	
	
	public static boolean eliminarDormitorio(String nDorm){
		boolean res = false;
		
		try {
			String str="call removeRoom(?)";
			CallableStatement stmt = DataBaseConection.conn.prepareCall(str);
			stmt.setString(1, nDorm);
			
			stmt.execute();
			res = true;
		    stmt.close();
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return res;
	}
	
	public static boolean eliminarServicio(String servicio){
		boolean res = false;
		
		try {				
			String str="DELETE FROM Caracteristicas WHERE NombreServ=CAST(? AS CHAR(10))";
			PreparedStatement stmt = DataBaseConection.conn.prepareStatement(str);
			stmt.setString(1, servicio);
			
			stmt.execute();
			res = true;
		    stmt.close();
		}catch(Exception e){
			System.out.println(e);
		}
		return res;
	}
	
	public static boolean actualizarServicio(String servicio, String nuevoPrecio){
		boolean res = false;
		
		try {
			String str="UPDATE Caracteristicas SET Precio=? WHERE NombreServ=TO_NUMBER(?)";
			PreparedStatement stmt = DataBaseConection.conn.prepareStatement(str);
			stmt.setString(1,nuevoPrecio);
			stmt.setString(2, servicio);

			stmt.executeUpdate();
			res = true;
		    stmt.close();
		}catch(Exception e){
			System.out.println(e);
		}
		return res;
	}

	public static boolean newCliente(String ndoc, String tipoDoc, String nombre, String apellido, String fechaNac, String sexo, String tel, String email){
		boolean res = false;
		
		tipoDoc = tipoDoc.toUpperCase();
		sexo = sexo.toUpperCase();
		
		try {
			String str="INSERT INTO Cliente (NDocumento,TipoDoc,Nombre,Apellido,FechaNac,Sexo,Telefono,Email) VALUES (TO_NUMBER(?),?,?,?,to_date(?,'DD/MM/YYYY'),?,?,?)";
			PreparedStatement stmt = DataBaseConection.conn.prepareStatement(str);
			stmt.setString(1, ndoc);
			stmt.setString(2, tipoDoc);
			stmt.setString(3, nombre);
			stmt.setString(4, apellido);
			stmt.setString(5, fechaNac);
			stmt.setString(6, sexo);
			stmt.setString(7, tel);
			stmt.setString(8, email);

			stmt.execute();
			res = true;
		    stmt.close();
		}catch(Exception e){
			System.out.println(e);
		}
		return res;
	}
	
}
